
const {ccclass, property, menu, requireComponent} = cc._decorator;

@ccclass
@menu("按钮/显示预制")
@requireComponent(cc.Button)
export default class ClickShowPrefab extends cc.Component {

    @property(cc.Prefab)
    prefab: cc.Prefab = null;

    @property(cc.Node)
    parent: cc.Node = null;

    start () {
        var clickEventHandler = new cc.Component.EventHandler();
        clickEventHandler.target = this.node; // 这个 node 节点是你的事件处理代码组件所属的节点
        clickEventHandler.component = "ClickShowPrefab";// 这个是代码文件名
        clickEventHandler.handler = "callback";
        clickEventHandler.customEventData = "";

        var button = this.node.getComponent(cc.Button);
        button.clickEvents.push(clickEventHandler);
    }

    callback() {
        let ins = cc.instantiate(this.prefab);
        let parent = this.parent || cc.Canvas.instance.node;
        ins.parent = parent;
    }
}
